Return to Home Page.
Today we’re making interactive plots in plotly. We’ll make examples using the NYC airbnb dataset.
Do some initial data cleaning / subsetting
data("nyc_airbnb")
nyc_airbnb =
nyc_airbnb |>
mutate(rating = review_scores_location / 2) |>
select(
rating, neighbourhood_group, neighbourhood,
room_type, price, lat, long) |>
drop_na(rating) |>
filter(
neighbourhood_group == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500
)
Use plotly to make some quick plots.
nyc_airbnb |>
mutate(text_label = str_c("Price: $", price, "\nNeighborhood: ", neighbourhood)) |>
plot_ly(
x = ~lat, y = ~long, color = ~price, text = ~text_label,
type = "scatter", mode = "markers", alpha = 0.5
)
Next up – boxplot.
nyc_airbnb |>
plot_ly(x = ~neighbourhood, y = ~price, color = ~neighbourhood,
type = "box", colors = "viridis")
Let’s do a bar chart with number of rentals.
nyc_airbnb |>
count(neighbourhood) |>
mutate(neighbourhood = fct_reorder(neighbourhood, n)) |>
plot_ly(x = ~neighbourhood, y = ~n,
type = "bar")
Another plot, with some volcano dataset.
plot_ly(
z = volcano,
type = "heatmap"
)
Choropleth
plot_ly(
type = "choropleth",
locations = c("AZ", "CA", "VT"),
locationmode = "USA-states",
colorscale = "Viridis",
z = c(10, 20, 40)
) |>
layout(geo = list(scope = "usa"))